﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Draco.OWF.Studio.Common.WPF;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Media;
using Draco.OWF.Studio.Utility;
using System.Windows.Input;

namespace Draco.OWF.Studio.Designer
{
    public class RouteFacet : Arrow
    {
        private Cursor m_PreviousCursor = Cursors.Arrow;
        public ActivityFacet AcFacetFrom { get; private set; }
        public ActivityFacet AcFacetTo { get; private set; }
        
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="acFrom"></param>
        /// <param name="acTo"></param>
        public RouteFacet(ActivityFacet acFrom,ActivityFacet acTo)
        {
            if (acFrom == null || acTo == null)
            {
                throw new ArgumentNullException();
            }
            this.HeadWidth = 10;
            this.HeadHeight = 5;
            this.StrokeThickness = 2;
            this.Stroke = Brushes.Black;
            this.LeftMouseClick += new System.Windows.Input.MouseButtonEventHandler(RouteFacet_MouseClick);
            this.MouseEnter += new System.Windows.Input.MouseEventHandler(RouteFacet_MouseEnter);
            this.MouseLeave += new System.Windows.Input.MouseEventHandler(RouteFacet_MouseLeave);

            ContextMenu CtxMenu = new System.Windows.Controls.ContextMenu();
            CtxMenu.Items.Add("1");
            CtxMenu.Items.Add("2");
            CtxMenu.Items.Add("3");
            this.ContextMenu = CtxMenu;

            AcFacetFrom = acFrom;
            AcFacetTo = acTo;
            AcFacetFrom.ActivityFacetMove += new ActivityFaceMoveEventHandler(AcFacet_Move);
            AcFacetTo.ActivityFacetMove += new ActivityFaceMoveEventHandler(AcFacet_Move);
            CalulateCoordinate();
        }

        #region Mouse Event
        void RouteFacet_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.m_PreviousCursor = this.Cursor;
            this.Cursor = Cursors.Pen;
        }
        void RouteFacet_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.Cursor = this.m_PreviousCursor;
        }

        void RouteFacet_MouseClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            MessageBox.Show("route clicked ");
        }
        #endregion

        private void AcFacet_Move(ActivityFacet face, ActivityFaceMoveEventArgs e)
        {
            CalulateCoordinate();
        }

        private void CalulateCoordinate()
        {
            Point p1 = new Point(AcFacetFrom.CenterPoint.X, AcFacetFrom.CenterPoint.Y);
            Point p2 = new Point(AcFacetTo.CenterPoint.X, AcFacetTo.CenterPoint.Y);

            double Theta = Math.Atan2(p1.Y - p2.Y, p1.X - p2.X);
            double DeltaX = 32 * Math.Cos(Theta);
            double DeltaY = 32 * Math.Sin(Theta);
            Point p3 = new Point(p2.X + DeltaX, p2.Y + DeltaY);

            this.X1 = p1.X;
            this.Y1 = p1.Y;
            this.X2 = p3.X;
            this.Y2 = p3.Y;
            Canvas.SetZIndex(this, -1);
        }
    }
}
